const { ccclass, property } = cc._decorator;

export enum TableType {
    Table_Type_GouMai = 0,
    Table_Type_JiaGe = 1
}

class TableCaheMap {
    /**
     * 所有的数据
     */
    public _tableArray: any = [];
    /**
     * 每行的数据
     */
    public _tableMap: Map<string, any> = new Map();
}

@ccclass
export default class LoadTable extends cc.Component {
    public static Instance: LoadTable = null;
    public static tableCaheMap: Map<TableType, TableCaheMap> = new Map();
    private tableDataPaths: any[] = [];  //存放txt路径

    onLoad() {
        LoadTable.Instance = this;
    }

    start() {
        this.initTablePath();
        this.schedule(this.startDataAnalysis);
    }

    private initTablePath() {
        this.tableDataPaths[TableType.Table_Type_GouMai] = 'tableData/GouMai';
        this.tableDataPaths[TableType.Table_Type_JiaGe] = 'tableData/JiaGe';
    }

    private curIndex: number = 0;
    private startDataAnalysis() {
        cc.resources.load(this.tableDataPaths[this.curIndex], cc.TextAsset, (err: Error, file: cc.TextAsset) => {
            if (err) {
                this.curIndex++;
                if (this.curIndex >= this.tableDataPaths.length) {
                    this.unschedule(this.startDataAnalysis);
                    return;
                }
            }
            else {
                if (LoadTable.tableCaheMap.has(this.curIndex as TableType) == false) {
                    LoadTable.tableCaheMap.set(this.curIndex as TableType, new TableCaheMap());
                }
                let tableCaheMap = LoadTable.tableCaheMap.get(this.curIndex as TableType);
                //按照第二行的属性为字段标识
                //对tableCaheMap进行赋值
                let textArray: string[] = file.text.split('\r\n');
                let flagArray: string[] = textArray[1].split('`');
                let typeArray: string[] = textArray[2].split('`');
                for (let i = 3, j = textArray.length; i < j; i++) {
                    let descObj: any = {};
                    flagArray.forEach((value, index) => {
                        let dataArray: string[] = textArray[i].split('`');
                        if (typeArray[index] == 'number') {
                            descObj[flagArray[index]] = Number(dataArray[index]);
                        }
                        else if (typeArray[index] == 'string') {
                            descObj[flagArray[index]] = dataArray[index];
                        }
                        tableCaheMap._tableMap.set(dataArray[0], descObj)
                    })
                    tableCaheMap._tableArray[i - 3] = descObj;
                }

                //判断是否停止调度器
                this.curIndex++;
                if (this.curIndex >= this.tableDataPaths.length) {
                    this.unschedule(this.startDataAnalysis);
                    return;
                }
            }
        });
    }

    //获取idKey数据
    public searchTable(tableType: TableType, idKey: string): any {
        let tableCaheMap: TableCaheMap = LoadTable.tableCaheMap.get(tableType);
        if (tableCaheMap == null) {
            return null;
        }

        return tableCaheMap._tableMap.get(idKey);
    }

    //获取全部数据
    public searchTables(tableType: TableType): any {
        let tableCaheMap: TableCaheMap = LoadTable.tableCaheMap.get(tableType);
        if (tableCaheMap == null) {
            return null;
        }

        return tableCaheMap._tableArray;
    }
}
